package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void addOrderSettings(List<OrderSetting> orderSettingList) {
        if(orderSettingList != null &&orderSettingList.size()>0){
            //1.循环
            for (OrderSetting orderSetting : orderSettingList) {
                //2.判断该日期是否进行了预约设置
                OrderSetting orderSettingDb = orderSettingDao.findByDate(orderSetting.getOrderDate());
                //2.1如果已设置执行该操作
                if(orderSettingDb!=null){
                    //注意：修改时需判断已预约人数（数据库）是否大于可预约人数（参数）
                    if(orderSettingDb.getReservations()>orderSetting.getNumber()){
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                        throw new RuntimeException("您设置的["
                                +sdf.format(orderSetting.getOrderDate())+"]已预约人数大于可预约，不能设置!!");
                    }else {
                        orderSettingDao.edit(orderSetting);
                    }
                }
                //如果没有设置，执行添加操作
                else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }
}
